package com.zara.store.client.view;

import java.util.Observable;
import java.util.logging.Logger;

import com.zara.store.client.clientmodel.EnvtModel;
import com.zara.store.client.controller.EnvtController;
import com.zara.store.client.view.swing.EnvtForm;
import com.zara.store.client.view.swing.table.EnvtTableModel;
import com.zara.store.common.ProductVO;

import framework.clientmodel.Model;
import framework.view.View;

public class EnvtView extends View {
	
	private EnvtForm envtForm;
	private static Logger LOG = Logger.getLogger(EnvtView.class.getName());

	public EnvtView(Model model) {
		super(model);
		EnvtController envtController = new EnvtController(model, this);
		setController(envtController);
		envtForm = new EnvtForm(this);
	}

	public void update(Observable arg0, Object arg1) {
		EnvtModel envtModel = (EnvtModel) getModel();
		
		EnvtTableModel envtTable = envtForm.getEnvtTableModel();
		EnvtTableModel envtPendingTable = envtForm.getEnvtPendingTableModel();

		envtPendingTable.clear();
		envtTable.clear();
		
		if(envtModel.hasEnvt()) {
			for(ProductVO p: envtModel.getItems()) {
				envtTable.addRow(columnFromVO(p));
			}
			if(envtModel.getPendingItems() != null) {
				for(ProductVO p: envtModel.getPendingItems()) {
					envtPendingTable.addRow(columnFromVO(p));
				}
			} else {
				LOG.severe("processing error, palcItem, palc, or product");
			}
		} else {
			LOG.severe("no envt");
		}
	}

	private Object[] columnFromVO(ProductVO p) {
		return new Object[]{p.getBarcode(), p.getName(), p.getAmount(), p.getStock()};
	}

	public void display() {
		envtForm.setVisible(true);
	}

	public EnvtForm getEnvtForm() {
		return envtForm;
	}

};
